import os
import re

os.chdir('..')
writelines = []
allcases = []
passed = []
failed = []
writelines.append(f'# cctest_report\n\n')
writelines.append(f'## 执行命令\n\n')
writelines.append(f'```bash\n')
writelines.append(f'make -j12 cctest | tee cctest.log\n')
writelines.append(f'```\n\n')
writelines.append(f'[生成测试报告使用的脚本](../script/process4.py)\n\n')

with open('log/cctest_log.md', 'r', encoding='utf-8') as f:
    lines = f.read()
    passedr = r'(?<=\[       OK \] ).*?(?= \()'
    failedr = r'(?<=\[  FAILED  \] ).*?(?= \()'

    passedcase = re.findall(passedr, lines)
    failedcase = re.findall(failedr, lines)
    allcases.extend(passedcase)
    allcases.extend(failedcase)


writelines.append(f'## 数量\n\n')
writelines.append(f'### Total tests: {len(allcases)}\n\n')
writelines.append(f'[log](../log/cctest_log.md)\n\n')

writelines.append(f'## 通过测试\n\n')
writelines.append(f'### Passed tests: {len(passedcase)}\n\n')
for i in passedcase:
    writelines.append(f'- {i}\n\n')

writelines.append(f'## 失败测试\n\n')
writelines.append(f'### Failed tests: {len(failedcase)}\n\n')
for i in failedcase:
    writelines.append(f'- {i}\n\n')

with open('report/cctest_report.md', 'w', encoding='utf-8') as f:
    f.writelines(writelines)
